<?php
ini_set("display_error", 1);
error_reporting(E_ALL);

$mcrTmStrt = microtime(true);
require_once("mix.func.php");
require_once("mbUser.class.php");
require_once("sdao/StructuredDataAccessObject.php");

$old_error_handler = set_error_handler("mbErrorHandler");

if (!isset($_REQUEST['xsn'])) {
	serverRedir("".SERVER_URL_ROOT."/app/_com/login.php?errMsg=9997");
	exit();
}
session_start();
$ax = explode('.',$_REQUEST['xsn']);
$subSess = $ax[0];
if (!isset($isPopUp)) $isPopUp = false;
if (!isset($isReloadable)) $isReloadable = true;
if (!isset($ax[1])) {
	if (!$isPopUp) {
		serverRedir("".SERVER_URL_ROOT."/app/_com/login.php?errMsg=9997");
		exit();
	}
	$pageSeq = 0;
} else {
	$pageSeq = intval($ax[1]);
}
$userObjKey = 'polarismb'.$subSess;
if(!isset($_SESSION[$userObjKey])){
	serverRedir("".SERVER_URL_ROOT."/app/_com/login.php?errMsg=9998");
	exit();
}
if($_SESSION[$userObjKey]->user == '@@logout@@'){
	serverRedir("".SERVER_URL_ROOT."/app/_com/login.php?errMsg=9996");
	exit();
}
$objUser = $_SESSION[$userObjKey];
if (!isset($objSDAO)) $objSDAO = new SDAO($aMetaConPar);
// check page sequence
if (!$isPopUp) {	
	if ($objUser->pageSequence != $pageSeq) {
		define('USEROBJ','polarismb'.$subSess);
		define('XSN',"$subSess.".$objUser->pageSequence);
		define('SUB_SESSION_NUMBER',$subSess);
		header("location: ".$objUser->getHistory(0,true));
		exit();
	} 
	$objUser->pageSequence++;
	$pageSeq = $objUser->pageSequence;
	if (!isset($noHistoryPost)) $noHistoryPost = false;
	if ($isReloadable) {
		if (isset($_REQUEST['hstrbck'])) { //history back
			$objUser->backHistory($_REQUEST['hstrbck']);
		} else {
			$objUser->pushHistory($noHistoryPost);
		}
	}
}
if (sxREQ('hstry')=='res') { //reset history
	$objUser->resetHistory();
}
define('USEROBJ','polarismb'.$subSess);
define('XSN',"$subSess.$pageSeq");
define('SUB_SESSION_NUMBER',$subSess);
// aggiorna tabella session
$sTimeNow = date('Y-m-d H:i:s');
$sSQL = "UPDATE session SET time_last = TIMESTAMP('$sTimeNow') " .
	"WHERE uniqid = '".$_SESSION[USEROBJ]->uniqid."' " ;	
$objSDAO->query($sSQL,"updSess",array("contOnErr"=>true));

function mbErrorHandler($errno, $errstr, $errfile, $errline){
	global $aDebugParam, $objUser;
   switch ($errno) {
      case E_USER_WARNING:
      case E_WARNING:
      case E_CORE_WARNING:
         if ((isset($aDebugParam['warning']) and $aDebugParam['warning'] === true) 
         and (!isset($aDebugParam['no_warning']) or $aDebugParam['no_warning'] !== true)) {
            dbg_("WARNING : $errstr in $errfile a linea $errline ");
         }
         return;
      case E_USER_NOTICE:
      case E_NOTICE:
         if ((isset($aDebugParam['notice']) and $aDebugParam['notice'] === true) 
         and (!isset($aDebugParam['no_warning']) or $aDebugParam['no_warning'] !== true)) {
            dbg_("NOTICE : $errstr in $errfile at line $errline ");
         }
         return;
      case E_STRICT:
         if ((isset($aDebugParam['strict']) and $aDebugParam['strict'] === true) 
         and (!isset($aDebugParam['no_warning']) or $aDebugParam['no_warning'] !== true)) {
            dbg_("STRICT : $errstr in $errfile at line $errline ");
         }
         return;
      case E_DEPRECATED:
         if ((isset($aDebugParam['strict']) and $aDebugParam['strict'] === true) 
         and (!isset($aDebugParam['no_warning']) or $aDebugParam['no_warning'] !== true)) {
            dbg_("DEPRECATED : $errstr in $errfile at line $errline ");
         }
         return;
   }
	if (headers_sent() 
	|| !isset($_SERVER["SERVER_NAME"])
	|| !is_object($objUser) 
	|| get_class($objUser)!='sxUser') {
				echo "<br>*** ABNORMAL-END ***<br>\n";			
		 		echo "(SX$errno";
	 			echo (headers_sent())?'-HS':'';
	 			echo (!isset($_SERVER["SERVER_NAME"]))?'-NSN':'';
	 			echo (!is_object($objUser))?'-NOU':'';
				echo ") $errstr in $errfile at line $errline ";
	 			echo "<br>\n";
            dbg_("Abnormal-end : $errstr in $errfile at line $errline <br>");
	 			exit(1);
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META Name="expires" CONTENT= "Mon, 1 Jan 2000 00:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="content-language" content="it">
<META HTTP-EQUIV="Content-Type" content="text/html; utf-8">
<TITLE>POLARIS - Abnormal end</TITLE>
<link rel="stylesheet" href="<?=SERVER_URL_ROOT?>/app/_com/stile.css.php" type="text/css" />
</HEAD>
<BODY>
  <table align='center'>
    <tr>
      <td colspan='2' class='titl01' valign='middle'>
      <img height='30' src="<?=SERVER_URL_ROOT?>/app/_img/attention.png" border='0' align='left'/>
      &nbsp;&nbsp;&nbsp;POLARIS - META-BASE<br/>
      &nbsp;&nbsp;&nbsp;ABNORMAL-END</td>
    </tr>
    <tr>
      <td class='labl01' width='100'>Error code</td><td class='text01' width='500'><?="$errno - $errstr"?></td>
    </tr>
    <tr>
      <td class='labl01' width='100'>Program</td><td class='text01' width='500'><?=$errfile?></td>
    </tr>
    <tr>
      <td class='labl01'>Line</td><td class='text01'><?=$errline?></td>
    </tr>
    <tr>
      <td class='labl01'>Date/time</td><td class='text01'><?=date('d/m/Y H:i:s')?></td>
    </tr>
    <tr>
      <td class='labl01'>Server</td><td class='text01'><?=$_SERVER["HTTP_HOST"]?></td>
    </tr>
    <tr>
      <td class='labl01'>Page</td><td class='text01'><?=$_SERVER["PHP_SELF"]?></td>
    </tr>
    <tr>
      <td class='labl01'>Query string</td><td class='text01'><?=$_SERVER["QUERY_STRING"]?></td>
    </tr>
    <tr>
      <td class='labl01'>Server software</td><td class='text01'><?=$_SERVER["SERVER_SOFTWARE"]?></td>
    </tr>
    <tr>
      <td class='labl01'>Remote address</td><td class='text01'><?=$_SERVER["REMOTE_ADDR"]?></td>
    </tr>
    <tr>
      <td colspan='2' align='right'>
<?
	if (defined('USEROBJ') 
	&& isset($_SESSION[USEROBJ]) 
	&& is_object($_SESSION[USEROBJ])) {
?>
			<a href="<?=$_SESSION[USEROBJ]->getHistory(-1,true)?>" class='link01'>
				<?=_t('previus page')?>
			</a>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?
	}
?>
			<a href="<?=SERVER_URL_ROOT?>/app/data/area/dataAreaList.php?xsn=<?=XSN?>" class='link01'>
				<?=_t('start page')?>
			</a>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<a href="<?=SERVER_URL_ROOT?>/app/_com/logout.php?xsn=<?=XSN?>" class='link01'>
				<?=_t('log-out')?>
			</a>
      </td>
    </tr>
    
  </table> 
</BODY>
</HTML>
<?php
	exit();
}
